﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.BasicList
{
    public partial class frm_Add_Staff : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        private string error;

        public frm_Add_Staff()
        {
            InitializeComponent();

            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.error = null;

            ComboboxItem item = new ComboboxItem();
            item.Text = "Male";
            item.Value = "1";
            this.sexValue.Properties.Items.Add(item);

            ComboboxItem item2 = new ComboboxItem();
            item2.Text = "Female";
            item2.Value = "2";
            this.sexValue.Properties.Items.Add(item2);
        }
        
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            error = null;
            string staff_id = null;
            string staff_name = null;
            string sec_id = null;
            string sec_name = null;
            DateTime birthday = new DateTime();
            DateTime hiredate = new DateTime();
            DateTime stopdate = new DateTime();
            int sex = 0;
            string groupid = null;
            string personalinf = null;
            string department = null;
            string education = null;
            string address = null;
            string phonenumber = null;

            if (string.IsNullOrEmpty(this.staffID.Text))
            {
                error = "Please enter Staff ID";
            }
            else
            {
                staff_id = this.staffID.Text;
            }
            if (string.IsNullOrEmpty(this.staffName.Text))
            {
                error = "Please enter Staff Name";
            }
            else
            {
                staff_name = this.staffName.Text;
            }
            if (string.IsNullOrEmpty(error))
            {
                sec_id = this.secID.Text;
                sec_name = this.secName.Text;
                groupid = this.groupID.Text;
                personalinf = this.personalInf.Text;
                if (this.sexValue.SelectedIndex == 0)
                {
                    sex = 1;
                }
                else
                {
                    sex = 0;
                }
                department = this.departmentValue.Text;
                education = this.educationValue.Text;
                address = this.addressValue.Text;
                phonenumber = this.phoneNumber.Text;
                            
                try
                {
                    myConnection.Open();
                    string sql = "INSERT INTO [222_Staff] ([StaffID], [StaffName], [Birthday], [HireDate],"
                        + " [StopDate], [SecID], [SecName], [GroupID], [PersonalInf], [Sex], [DepartmentID],"
                        + " [Education], [PhoneNumber], [Address])"
                        + " VALUES ('" + staff_id + "', N'" + staff_name + "', @birthday, @hiredate, @stopdate, '" + sec_id + "',"
                        + " '" + sec_name + "', '" + groupid + "', '" + personalinf + "', " + sex + ", '" + department + "',"
                        + " '" + education + "', '" + phonenumber + "', '" + address + "')";
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                    if (this.birthDay.EditValue != null)
                    {
                        birthday = DateTime.Parse(this.birthDay.EditValue.ToString());
                        cmd_sql.Parameters.Add("@birthday", SqlDbType.DateTime).Value = birthday.Date;
                    }
                    else
                    {
                        cmd_sql.Parameters.Add("@birthday", SqlDbType.DateTime).Value = DBNull.Value;
                    }
                    if (this.hireDate.EditValue != null)
                    {
                        hiredate = DateTime.Parse(this.hireDate.EditValue.ToString());
                        cmd_sql.Parameters.Add("@hiredate", SqlDbType.DateTime).Value = hiredate.Date;
                    }
                    else
                    {
                        cmd_sql.Parameters.Add("@hiredate", SqlDbType.DateTime).Value = DBNull.Value;
                    }                    
                    if (this.stopDate.EditValue != null)
                    {
                        stopdate = DateTime.Parse(this.stopDate.EditValue.ToString());
                        cmd_sql.Parameters.Add("@stopdate", SqlDbType.DateTime).Value = stopdate.Date;
                    }
                    else
                    {
                        cmd_sql.Parameters.Add("@stopdate", SqlDbType.DateTime).Value = DBNull.Value;
                    }                    
                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();
                    MessageBox.Show("Staff added success.");
                    clearForm();
                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    //MessageBox.Show(ex.Message);
                    MessageBox.Show("Cannot add Staff. Please try again.");
                }
            }
            else
            {
                MessageBox.Show(error);
            }
        }

        private void clearForm()
        {
            foreach (Control c in this.Controls)
            {
                if (c is DevExpress.XtraEditors.TextEdit)
                {
                    ((DevExpress.XtraEditors.TextEdit)c).Text = null;
                }

                if (c is DevExpress.XtraEditors.ComboBoxEdit)
                {
                    ((DevExpress.XtraEditors.ComboBoxEdit)c).SelectedIndex = -1;
                }

                if (c is System.Windows.Forms.MaskedTextBox)
                {
                    ((System.Windows.Forms.MaskedTextBox)c).Text = null;
                }
            }
        }
    }
}
